package newclasses;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Vector;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;

import src.RTPpacket;

public class SoundRead {

	
	private Vector<Byte> samples ;
	private AudioFormat	audioFormat ;
	private static final int	EXTERNAL_BUFFER_SIZE = 100;
	
	public SoundRead() {
		this.samples = new Vector<Byte>();
	}	
		
	public Vector<RTPpacket>  convertToPackets(int lenghtPackt) {
		
		Iterator<Byte> i = this.samples.iterator();
		int count = 0;
		byte[] data = new byte[lenghtPackt];
		Vector<RTPpacket> packets = new Vector<RTPpacket>();
		int npacket = 0;
		
		double milisecondsPerPacket = (lenghtPackt /(audioFormat.getFrameRate()*audioFormat.getFrameSize()) ) * 1000;
		
		while(i.hasNext())
		{
			data[count++] = i.next(); 
			
			if(count == lenghtPackt)
			{
				count = 0;
				packets.add(new RTPpacket(10,npacket,(int)(npacket * milisecondsPerPacket),data,lenghtPackt) );
				npacket++;
			}
		}
		
		if(count > 0)
		{
			byte[] temp = new byte[count];
			
			for (int j = 0; j < temp.length; j++) {
				temp[j] = data[j];
			}
			
			packets.add(new RTPpacket(10,npacket,(int)(npacket * milisecondsPerPacket),temp,count));
		}
		
		
		return packets;
		
	}
	
	public Vector<Byte> convertPacketsToVector(Vector<RTPpacket> packets,int lenghtPacket) {
		
		Iterator<RTPpacket> i = packets.iterator();
		byte data[] = new byte[lenghtPacket];
		Vector<Byte> samples = new Vector<Byte>();
		RTPpacket p;
		int length = 0;
		
		while(i.hasNext())
		{
			p = i.next();
			p.getpayload(data);
			length = p.getpayload_length();

			for (int j = 0; j < length ;  j++) {
				samples.add(data[j]);
			}
		}
				
		return samples;		
	}
	
	public void readFileSound(String strFilename) {
		
		File	soundFile = new File(strFilename);
		AudioInputStream	audioInputStream = null;
		
		try
		{
			audioInputStream = AudioSystem.getAudioInputStream(soundFile);
		}
		catch (Exception e)
		{
			
			e.printStackTrace();
			System.exit(1);
		}
			audioFormat = audioInputStream.getFormat();

		int	nBytesRead = 0;
		byte[]	abData = new byte[EXTERNAL_BUFFER_SIZE];
		int index = 0;
		
		while (nBytesRead != -1)
		{
			try
			{
				nBytesRead = audioInputStream.read(abData, 0, abData.length);
				
				for (int i = 0; i < nBytesRead ; i++) {
					samples.add(index+i,abData[i]);
				}
				index += nBytesRead;
				
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
			
		}
	}
	
	public int bytesPerMilisecondes(){
		return (int)(audioFormat.getFrameRate()*audioFormat.getFrameSize()/1000);
	}
	public void writeFileSound(Vector<Byte> samples) {
		
		SourceDataLine	line = null;
		DataLine.Info	info = new DataLine.Info(SourceDataLine.class,
												 audioFormat);
		try
		{
			line = (SourceDataLine) AudioSystem.getLine(info);
			line.open(audioFormat);
		}
		catch (LineUnavailableException e)
		{
			e.printStackTrace();
			System.exit(1);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			System.exit(1);
		}

	
		line.start();


		int	nBytesRead = 0;
		byte[]	abData = new byte[EXTERNAL_BUFFER_SIZE];
		int index = 0;
		

		Iterator<Byte> i = samples.iterator();
		
		int count = 0;
		
		while(i.hasNext())
		{
			abData[count++] = i.next();
			
			if(count == abData.length)
			{
				int	nBytesWritten = line.write(abData, 0, count);
				count = 0;
			}	
		}
		
		int	nBytesWritten = line.write(abData, 0, count);
		
		
		line.drain();
		line.close();


	}
	
	
	
	public AudioFormat getAudioFormat() {
		return audioFormat;
	}

	public void setAudioFormat(AudioFormat audioFormat) {
		this.audioFormat = audioFormat;
	}

	public static void main(String[] args) {
		
		
		/*SoundRead sound = new SoundRead();
		
		Vector<Byte> v = sound.readFileSound("voz.wav");
		
		Vector<RTPpacket> p  = sound.convertToPackets(100);
		
		for (int i = 0; i < 100; i++) {
			p.remove(i + 10);
		}
		
		
		v = sound.convertPacketsToVector(p,100);
		//System.out.println(v.size());
		//System.out.println(v.get(v.size()-1));
		sound.writeFileSound(v);*/
	}
	
}
